﻿--[[
   @brief    游戏结束后退弹窗口
   @author   hanmanhuang
   @version  1.0
   @date     2017-04-12
--]]


game_end_popwnd_control = {
    popwnd_cfg_name = "game_end_pop_wnd_cfg.js";
    popwnd_cfg_info = {};

    popwnd_show_cfg_list = {};

    init = function()
        game_end_popwnd_control.inner_log("init");
        ierd_tgp.tpf_ui.Require("common\\OssConfig.lua");
    end;
    
    inner_log = function (msg, core_msg)
        base_utility.log(string.format("[game_end_popwnd_control] %s", msg), core_msg);
    end;

    OnLoginSuccess = function()
        -- 拉取退弹配置
        game_end_popwnd_control.inner_log("begin get config");
        TOssConfig.QueryCfg(game_end_popwnd_control.popwnd_cfg_name, "game_end_popwnd_control.OnGetJsonCfg", false);
    end;

    OnGetJsonCfg = function(sFile, zJson)
        if (nil == zJson) or (nil == sFile) or (game_end_popwnd_control.popwnd_cfg_name ~= sFile) then
            game_end_popwnd_control.inner_log("get config failed");
            return;
        end;

        local zData = zJson["data"];
        if (nil == zData) then
            game_end_popwnd_control.inner_log("Get data fail.");
            return;
        end;
        game_end_popwnd_control.popwnd_cfg_info = zData;
    end;

    OnGameEnd = function(event_id, json)
        if base_utility.is_big_picture_ui() then
            return;
        end
        local json_param = base_utility.json_to_table(json);
        if json_param ~= nil and json_param.game_id ~= nil then
            for index, item in pairs(game_end_popwnd_control.popwnd_cfg_info) do
                if item ~= nil and item.game_id ~= nil and tonumber(item.game_id) == tonumber(json_param.game_id) then
                    local str_game_id = tostring(item.game_id);
                    if game_end_popwnd_control.popwnd_show_cfg_list[str_game_id] == nil then
                        game_end_popwnd_control.popwnd_show_cfg_list[str_game_id] = {};
                        game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].cfg = {};
                        game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].tick_show = false;
                    end;
                    if game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].tick_show == false then
                        game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].cfg = item;
                        game_end_popwnd_control.TickToShowWnd(str_game_id);
                    end;
                    break;
                end;
            end;
        end;
    end;

    TickToShowWnd = function(game_id)
        local str_game_id = tostring(game_id);
        if game_end_popwnd_control.popwnd_show_cfg_list[str_game_id] ~= nil and
           game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].tick_show == false then
            local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
            if nil ~= tpfui then
                tpfui:AddInterval("game_end_popwnd_control.PopGameEndWnd", 2*1000, tonumber(str_game_id));
                game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].tick_show = true;
                game_end_popwnd_control.inner_log("TickToShowWnd: "..str_game_id);
            end;
        end;
    end;

    EndTick = function(game_id)
        local str_game_id = tostring(game_id);
        if game_end_popwnd_control.popwnd_show_cfg_list[str_game_id] ~= nil and
           game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].tick_show == true then
            local tpfui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
            if nil ~= tpfui then
                tpfui:RemoveInterval("game_end_popwnd_control.PopGameEndWnd", tonumber(game_id));
                game_end_popwnd_control.popwnd_show_cfg_list[str_game_id].tick_show = false;
                game_end_popwnd_control.inner_log("EndTick: "..str_game_id);
            end;
        end;
    end;

    PopGameEndWnd = function(game_id)
        game_end_popwnd_control.EndTick(game_id);
        info = game_end_popwnd_control.popwnd_show_cfg_list[tostring(game_id)].cfg;
        if info ~= nil and info.url ~= nil and info.width ~= nil and info.height ~= nil then
            local param_tbl = {};
            param_tbl.url = info.url;
            param_tbl.width = tonumber(info.width);
            param_tbl.height = tonumber(info.height);
            param_tbl.use_ptlogin = 1;
            param_tbl.uniquekey = "game_end_pop_wnd"..tostring(info.game_id);
            param_tbl.position = info.position;
            local json = base_utility.table_to_json(param_tbl);
            event_center.send_event(browser_event.OPEN_IND_WEB_WND, json);
        end;
    end;
};

game_end_popwnd_control.init();